package com.llxy.web.controller.worker;
import com.llxy.common.annotation.Log;
import com.llxy.common.core.controller.BaseController;
import com.llxy.common.core.domain.AjaxResult;
import com.llxy.common.core.page.TableDataInfo;
import com.llxy.common.enums.BusinessType;
import com.llxy.common.utils.SecurityUtils;
import com.llxy.worker.dto.SupportWorkerDTO;
import com.llxy.worker.domain.SupportWorker;
import com.llxy.worker.service.SupportWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* (t_support_worker)表控制层
*
* @author 马瑞龙
*/
@RestController
@RequestMapping("/worker/support")
public class SupportWorkerController extends BaseController {
    /**
    * 服务对象
    */
    @Autowired
    private SupportWorkerService supportWorkerService;

    /**
     * 获取护工列表
     */
    @PreAuthorize("@ss.hasPermi('worker:support:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupportWorker supportWorker)
    {
        startPage();
        List<SupportWorkerDTO> supportWorkerList = supportWorkerService.selectSupportWorkerList(supportWorker);

        return getDataTable(supportWorkerList,supportWorkerService.selectSupportWorkerListCount(supportWorker));
    }

    /**
     * 获取未培训护工列表
     */
    @PreAuthorize("@ss.hasPermi('worker:support:list')")
    @GetMapping("/statusIsZoneList")
    public AjaxResult statusIsZoneList()
    {
        if (SecurityUtils.isAdmin(getUserId())){
            return success(supportWorkerService.selectAllStatusIsZoneList());
        }
        return success(supportWorkerService.selectStatusIsZoneList(getUserId()));
    }


    /**
     * 新增护工
     */
    @PreAuthorize("@ss.hasPermi('worker:support:add')")
    @Log(title = "护工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SupportWorker supportWorker)
    {
        supportWorker.setCreateBy(getUsername());
        supportWorker.setUserId(getUserId());
        SupportWorker sw = supportWorkerService.selectByUserId(supportWorker.getUserId());
        if (sw != null){
            return error("已经提交过了！请等待培训机构审核培训！");
        }
        return success(supportWorkerService.insertSupportWorker(supportWorker));
    }

    /**
     * 修改护工
     */
    @PreAuthorize("@ss.hasPermi('worker:support:edit')")
    @Log(title = "护工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SupportWorker supportWorker)
    {
        supportWorker.setUpdateBy(getUsername());
        return toAjax(supportWorkerService.updateByPrimaryKeySelective(supportWorker));
    }

    /**
     * 根据护工编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('worker:support:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id)
    {
        return success(supportWorkerService.selectByPrimaryKey(id));
    }

    /**
     * 删除护工
     */
    @PreAuthorize("@ss.hasPermi('worker:support:remove')")
    @Log(title = "护工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id)
    {
        return toAjax(supportWorkerService.deleteByPrimaryKey(id));
    }

    /**
     * 修改 推荐护工 状态
     */
    @PreAuthorize("@ss.hasPermi('worker:support:status')")
    @Log(title = "护工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/recommend/{id}/{status}")
    public AjaxResult updateRecommendStatus(@PathVariable Integer id, @PathVariable String status)
    {
        return toAjax(supportWorkerService.updateRecommendStatus(id,status));
    }

    /**
     * 修改 优秀员工 状态
     */
    @PreAuthorize("@ss.hasPermi('worker:support:status')")
    @Log(title = "护工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/outstanding/{id}/{status}")
    public AjaxResult updateOutstandingStatus(@PathVariable Integer id, @PathVariable String status)
    {
        return toAjax(supportWorkerService.updateOutstandingStatus(id,status));
    }


    @GetMapping("/count")
    public AjaxResult count()
    {
        return success(supportWorkerService.selectSupportWorkerListCount(null));
    }

}
